package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.R;
import com.example.entity.Category;
import com.example.entity.Dish;
import com.example.entity.Setmeal;
import com.example.entity.ShoppingCart;
import com.example.service.CategoryService;
import com.example.service.impl.DishServiceImpl;
import com.example.service.impl.SetmealServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 菜品及套餐分类(Category)表控制层
 *
 * @author makejava
 * @since 2022-05-23 09:36:42
 */
@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {
    /**
     * 服务对象
     */
    @Autowired
    private SetmealServiceImpl setmealService;
    @Resource
    private DishServiceImpl dishService;
    @Resource
    private CategoryService categoryService;


    @GetMapping("/page")//分页获取信息
    public R<Page> pageCategory(int page, int pageSize) {
        Page pageinfo = new Page(page, pageSize);
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.orderByAsc(Category::getSort);
        categoryService.page(pageinfo, queryWrapper);
        if (pageinfo != null) {
            return R.success(pageinfo);
        }
        return R.error("发生未知错误");

    }

    @PostMapping
    public R<String> postCategory(@RequestBody Category category) {//新增菜品
        boolean a = categoryService.save(category);
        if (a) {
            return R.success("成功");
        } else {
            return R.error("发生了未知错误请联系技术人员处理");
        }

    }

    @DeleteMapping
    public R<String> deleteCategory(Long ids) {
        System.out.println(ids);
        LambdaQueryWrapper<Dish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Dish::getCategoryId, ids);
        List<Dish> list = dishService.list(lambdaQueryWrapper);
        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper2 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper2.eq(Setmeal::getCategoryId, ids);
        List<Setmeal> list2 = setmealService.list(lambdaQueryWrapper2);
        boolean a = categoryService.deleteById(ids);
        if (a && list.size() <= 0 && list2.size() <= 0) {
            return R.success("成功");
        } else if (list.size() > 0) {
            return R.error("删除失败该菜系中还存有菜品");
        } else if (list2.size() > 0) {
            return R.error("删除失败该套餐中还存有菜品");
        } else {
            return R.error("未知错误");
        }

    }
    @PutMapping
    public R<String> putCategory(@RequestBody Category category){
        LambdaQueryWrapper<Category> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getId,category.getId());
        Category category1=categoryService.getOne(queryWrapper);
        category1.setSort(category.getSort());
        category1.setName(category.getName());
        boolean a=categoryService.updateById(category1);
        if(a){
            return R.success("修改成功");
        }else{
            return R.error("未知错误");
        }


    }
    @GetMapping("/list")
    public R<List<Category>> selectList(String type){
        LambdaQueryWrapper<Category>queryWrapper=new LambdaQueryWrapper<>();
        int a=0;
        if (StringUtils.isNotBlank(type)){
            a=Integer.parseInt(type);
        }
        queryWrapper.eq(StringUtils.isNotBlank(type),Category::getType,a);
        List<Category>categories=categoryService.list(queryWrapper);
        return R.success(categories);
    }

}